<?php
if($_SESSION['group_id']!=3){redir("./");exit;}

$res=mysql_query("select * from course_regis where course_id='$_GET[course_id]' and m_id='$_SESSION[m_id]' and status_id=1");
if(mysql_num_rows($res)==0){redir('./');exit;}
$course=mysql_fetch_assoc(mysql_query("select * from course where course_id='$_GET[course_id]'"));

if($_GET['op']==''){
?>
<div class="box">
  <h3>บทเรียนวิชา : <?php echo $course['course_name'];?></h3>
  <div>
  	<form method="get">
		<input name="search" value="<?php echo $search;?>" />
		<input type="hidden" name="mod" value="<?php echo $mod;?>" />
		<input type="hidden" name="course_id" value="<?php echo $_GET['course_id'];?>" />
		<input type="submit" value="ค้นหา" />
	</form>
  </div>
  <div class="boxContent">
  <?php
  $where="where course_id='$_GET[course_id]' and lesson_name like '%$search%'";
  $sql="select * from lesson $where order by lesson_name asc";
  $res=mysql_query($sql) or die(mysql_error());
  if(mysql_num_rows($res)>0){
	  echo "<ul>";
	  echo "<li>บทเรียน<ul>";
	  while($row=mysql_fetch_assoc($res))
	  {
		  echo "<li><a href=?mod=s.learn&lesson_id=$row[lesson_id] target=_blank title='เข้าสู่บทเรียน'>$row[lesson_name]</a></li>";
	  }
	  echo "</ul></li>";
	  
	  $where="where course_id='$_GET[course_id]' and lesson_name like '%$search%'";
	  $sql="select * from lesson $where order by lesson_name asc";
	  $res=mysql_query($sql) or die(mysql_error());
	  
	  echo "<li>แบบทดสอบ<ul>";
	  while($row=mysql_fetch_assoc($res))
	  {
		  $ex=mysql_fetch_assoc(mysql_query("select count(*) as num from examination where lesson_id='$row[lesson_id]' and m_id='$_SESSION[m_id]'"));
		  
		  if($row['have_exam']!=0 && $ex['num']==0 && $row['exam_open']==1 )
		  	echo "<li><a href=?mod=s.examination&lesson_id=$row[lesson_id] title='คลิกเพื่อทำแบบทดสอบ'>$row[lesson_name]</a></li>";
		  else if($row['have_exam']!=0)
		  	echo "<li>$row[lesson_name]</li>";
			
	  }
	  echo "</ul></li>";
	  
	  echo "</ul>";
  }else{
  	echo "<div id=error_msg>ไม่พบข้อมูล</div>";
  }
  ?>
  </div>
</div>
<?php
}else if($_GET['op']=='add'){
	if($_POST['op']=='add'){
		$lesson_name=$_POST['lesson_name'];
		$lesson_detail=$_POST['lesson_detail'];
		$question_num=$_POST['question_num'];
		$bH=$_POST['bH'];
		$bi=$_POST['bi'];
		$eH=$_POST['eH'];
		$ei=$_POST['ei'];
		$exam_start="$_POST[start] $bH:$bi:00";
		$exam_stop="$_POST[stop] $eH:$ei:00";
		$time_start=strtotime($exam_start);
		$time_stop=strtotime($exam_stop);
		$time_now=time();
		
		if(!$lesson_name)$err="กรุณากรอก ชื่อบทเรียน";
		else if(intval($question_num)==0)$err="จำนวนข้อสอบไม่ถูกต้อง";
		else if(!$time_start)$err="กรุณากรอก วันเวลาที่เริ่มสอบ";
		else if(!$time_stop)$err="กรุณากรอก วันเวลาที่สิ้นสุดการสอบ";
		else if($time_start>$time_stop)$err="ช่วงเวลาไม่ถูกต้อง";
		else if(!$lesson_detail)$err="กรุณากรอก เนื้อหา";
		else{
			$sql="insert into lesson(lesson_name,lesson_detail,question_num,exam_start,exam_stop,course_id) values('$lesson_name','$lesson_detail','$question_num','$exam_start','$exam_stop','$_GET[course_id]')";
			mysql_query($sql);
			redir("?mod=$mod&course_id=$_GET[course_id]",5);
			die("<div id=com_msg>เพิ่มบทเรียนเรียบร้อยแล้วกรุณารอสักครู่ <img src=images/load.gif></div>");
		}
	}
	if(!$start)$start=date('Y-m-d',time()+86400);
	if(!$stop)$stop=date('Y-m-d',time()+(86400*2));
	if(!$bH)$bH=date('H',time()+86400);
	if(!$bi)$bi=date('i',time()+86400);
	if(!$eH)$eH=date('H',time()+86400);
	if(!$ei)$ei=date('i',time()+86400);
?>
<style type="text/css">@import "js/datepicker/jquery.datepick.css";</style> 
<script type="text/javascript" src="js/datepicker/jquery.datepick.js"></script>
<script type="text/javascript" src="js/datepicker/jquery.datepick-th.js"></script>
<script>
$(document).ready(function(){
	$('#start').datepick({dateFormat: 'yyyy-mm-dd'});
	$('#stop').datepick({dateFormat: 'yyyy-mm-dd'});
});
</script>
<div class="box">
	<h2>เพิ่มบทเรียน</h2>
	<?php if($err)echo "<div id=error_msg>$err</div>";?>
	<div class="boxContent">
		<p>
	  <form method="post" enctype="multipart/form-data">
<table width="99%" border="0" cellspacing="2" cellpadding="2">
	    		<tr>
		  			<td width="180"><strong>ชื่อบทเรียน</strong></td>
					<td><input name="lesson_name" type="text" id="lesson_name" value="<?php echo $lesson_name;?>" /></td>
	    		</tr>
				<tr>
		  			<td width="180"><strong>จำนวนข้อสอบ</strong></td>
					<td><input name="question_num" type="text" id="question_num" value="<?php echo $question_num;?>" /></td>
	    		</tr>
				<tr>
		  			<td width="180"><strong>วันเวลาที่เริ่มสอบ</strong></td>
					<td>
					<input name="start" type="text" id="start" value="<?php echo $start;?>" />
					<?php
					echo "<select name=bH>";
					for($i=0;$i<24;++$i){
						$select=($i==$bH)?' selected ':'';
						echo "<option value=$i $select >$i</option>";
					}
					echo "</select>";
					?>:
					<?php
					echo "<select name=bi>";
					for($i=0;$i<60;++$i){
						$select=($i==$bi)?' selected ':'';
						echo "<option value=$i $select >$i</option>";
					}
					echo "</select>";
					?> น.					</td>
	    		</tr>
				<tr>
		  			<td width="180"><strong>วันเวลาที่สิ้นสุดการสอบ</strong></td>
					<td>
					<input name="stop" type="text" id="stop" value="<?php echo $stop;?>" />
					<?php
					echo "<select name=eH>";
					for($i=0;$i<24;++$i){
						$select=($i==$eH)?' selected ':'';
						echo "<option value=$i $select >$i</option>";
					}
					echo "</select>";
					?>:
					<?php
					echo "<select name=ei>";
					for($i=0;$i<60;++$i){
						$select=($i==$ei)?' selected ':'';
						echo "<option value=$i $select >$i</option>";
					}
					echo "</select>";
					?> น.					</td>
	    		</tr>
				<tr>
				  <td><strong>เนื้อหา</strong></td>
				  <td>&nbsp;</td>
        		</tr>
				<tr>
				  <td colspan="2" align="center">
				  <textarea name="lesson_detail"><?php echo $lesson_detail;?></textarea>
                      <script type="text/javascript" src="ckeditor/ckeditor.js"></script>
                      <script type="text/javascript">
                        //<![CDATA[
                            CKEDITOR.replace( 'lesson_detail',{
                
                            skin            : 'kama',
                            language        : 'th',
                            height            : 400,
                            width            : 650,
                
                            toolbar :
                        [
                            ['Source','-','Save','NewPage','Preview'],
                            ['Cut','Copy','Paste','PasteText','PasteFromWord'],
                            ['Undo','Redo','-','Find','Rebeauty','-','SelectAll','RemoveFormat'],
                            '/',
                            ['Bold','Italic','Underline','Strike','-','Subscript','Superscript'],
                            ['NumberedList','BulletedList','-','Outdent','Indent','Blockquote'],
                            ['JustifyLeft','JustifyCenter','JustifyRight','JustifyBlock'],
                            ['Link','Unlink','Anchor'],
                            ['Image','Flash','Table','HorizontalRule','Smiley','SpecialChar'],
                            '/',
                            ['Format','Font','FontSize'],
                            ['TextColor','BGColor'],
                            ['Maximize', 'ShowBlocks']
                
                        ], 
                
                            filebrowserBrowseUrl : 'ckfinder/ckfinder.html',
                            filebrowserImageBrowseUrl : 'ckfinder/ckfinder.html?Type=Images',
                            filebrowserFlashBrowseUrl : 'ckfinder/ckfinder.html?Type=Flash',
                            filebrowserUploadUrl : 'ckfinder/core/connector/php/connector.php?command=QuickUpload&type=Files',
                            filebrowserImageUploadUrl : 'ckfinder/core/connector/php/connector.php?command=QuickUpload&type=Images',
                            filebrowserFlashUploadUrl : 'ckfinder/core/connector/php/connector.php?command=QuickUpload&type=Flash'
                
                            } );
                        //]]>
                    </script>
					</td>
	    		</tr>
				<tr>
				  <td>&nbsp;</td>
				  <td>&nbsp;</td>
	    </tr>
  		</table>
			<table width="99%" border="0" cellspacing="2" cellpadding="2">
	    		<tr>
		  			<td align="center"><input type="submit" class="button" name="Submit" value="เพิ่มบทเรียน" />
	  			    <input name="op" type="hidden" id="op" value="add" /></td>
	    		</tr>
	  		</table>
	  </form>
		</p>
	</div>
</div>
<?php
}else if($_GET['op']=='edit'){
	if($_POST['op']=='update'){
		$lesson_name=$_POST['lesson_name'];
		$lesson_detail=$_POST['lesson_detail'];
		$question_num=$_POST['question_num'];
		$bH=$_POST['bH'];
		$bi=$_POST['bi'];
		$eH=$_POST['eH'];
		$ei=$_POST['ei'];
		$exam_start="$_POST[start] $bH:$bi:00";
		$exam_stop="$_POST[stop] $eH:$ei:00";
		$time_start=strtotime($exam_start);
		$time_stop=strtotime($exam_stop);
		$time_now=time();
		
		if(!$lesson_name)$err="กรุณากรอก ชื่อบทเรียน";
		else if(intval($question_num)==0)$err="จำนวนข้อสอบไม่ถูกต้อง";
		else if(!$time_start)$err="กรุณากรอก วันเวลาที่เริ่มสอบ";
		else if(!$time_stop)$err="กรุณากรอก วันเวลาที่สิ้นสุดการสอบ";
		else if($time_start>$time_stop)$err="ช่วงเวลาไม่ถูกต้อง";
		else if(!$lesson_detail)$err="กรุณากรอก เนื้อหา";
		else{
			$sql="update lesson set lesson_name='$lesson_name',lesson_detail='$lesson_detail',question_num='$question_num',exam_start='$exam_start',exam_stop='$exam_stop' where lesson_id='$_GET[lesson_id]'";
			mysql_query($sql);
			redir("?mod=$mod&course_id=$_GET[course_id]",5);
			die("<div id=com_msg>ปรับปรุงบทเรียนเรียบร้อยแล้วกรุณารอสักครู่ <img src=images/load.gif></div>");
		}
	}
	$row=mysql_fetch_assoc(mysql_query("select * from lesson where lesson_id='$_GET[lesson_id]'"));
	$start=substr($row['exam_start'],0,10);
	$stop=substr($row['exam_stop'],0,10);
	$bH=substr($row['exam_start'],11,2);
	$bi=substr($row['exam_start'],14,2);
	$eH=substr($row['exam_stop'],11,2);
	$ei=substr($row['exam_stop'],14,2);
	
?>
<style type="text/css">@import "js/datepicker/jquery.datepick.css";</style> 
<script type="text/javascript" src="js/datepicker/jquery.datepick.js"></script>
<script type="text/javascript" src="js/datepicker/jquery.datepick-th.js"></script>
<script>
$(document).ready(function(){
	$('#start').datepick({dateFormat: 'yyyy-mm-dd'});
	$('#stop').datepick({dateFormat: 'yyyy-mm-dd'});
});
</script>
<div class="box">
	<h2>แก้ไขบทเรียน</h2>
	<?php if($err)echo "<div id=error_msg>$err</div>";?>
	<div class="boxContent">
		<p>
	  <form method="post" enctype="multipart/form-data">
<table width="99%" border="0" cellspacing="2" cellpadding="2">
	    		<tr>
		  			<td width="180"><strong>ชื่อบทเรียน</strong></td>
					<td><input name="lesson_name" type="text" id="lesson_name" value="<?php echo $row['lesson_name'];?>" /></td>
	    		</tr>
				<tr>
		  			<td width="180"><strong>จำนวนข้อสอบ</strong></td>
					<td><input name="question_num" type="text" id="question_num" value="<?php echo $row['question_num'];?>" /></td>
	    		</tr>
				<tr>
		  			<td width="180"><strong>วันเวลาที่เริ่มสอบ</strong></td>
					<td>
					<input name="start" type="text" id="start" value="<?php echo $start;?>" />
					<?php
					echo "<select name=bH>";
					for($i=0;$i<24;++$i){
						$select=($i==$bH)?' selected ':'';
						echo "<option value=$i $select >$i</option>";
					}
					echo "</select>";
					?>:
					<?php
					echo "<select name=bi>";
					for($i=0;$i<60;++$i){
						$select=($i==$bi)?' selected ':'';
						echo "<option value=$i $select >$i</option>";
					}
					echo "</select>";
					?> น.					</td>
	    		</tr>
				<tr>
		  			<td width="180"><strong>วันเวลาที่สิ้นสุดการสอบ</strong></td>
					<td>
					<input name="stop" type="text" id="stop" value="<?php echo $stop;?>" />
					<?php
					echo "<select name=eH>";
					for($i=0;$i<24;++$i){
						$select=($i==$eH)?' selected ':'';
						echo "<option value=$i $select >$i</option>";
					}
					echo "</select>";
					?>:
					<?php
					echo "<select name=ei>";
					for($i=0;$i<60;++$i){
						$select=($i==$ei)?' selected ':'';
						echo "<option value=$i $select >$i</option>";
					}
					echo "</select>";
					?> น.					</td>
	    		</tr>
				<tr>
				  <td><strong>เนื้อหา</strong></td>
				  <td>&nbsp;</td>
        		</tr>
				<tr>
				  <td colspan="2" align="center">
				  <textarea name="lesson_detail"><?php echo $row['lesson_detail'];?></textarea>
                      <script type="text/javascript" src="ckeditor/ckeditor.js"></script>
                      <script type="text/javascript">
                        //<![CDATA[
                            CKEDITOR.replace( 'lesson_detail',{
                
                            skin            : 'kama',
                            language        : 'th',
                            height            : 400,
                            width            : 650,
                
                            toolbar :
                        [
                            ['Source','-','Save','NewPage','Preview'],
                            ['Cut','Copy','Paste','PasteText','PasteFromWord'],
                            ['Undo','Redo','-','Find','Rebeauty','-','SelectAll','RemoveFormat'],
                            '/',
                            ['Bold','Italic','Underline','Strike','-','Subscript','Superscript'],
                            ['NumberedList','BulletedList','-','Outdent','Indent','Blockquote'],
                            ['JustifyLeft','JustifyCenter','JustifyRight','JustifyBlock'],
                            ['Link','Unlink','Anchor'],
                            ['Image','Flash','Table','HorizontalRule','Smiley','SpecialChar'],
                            '/',
                            ['Format','Font','FontSize'],
                            ['TextColor','BGColor'],
                            ['Maximize', 'ShowBlocks']
                
                        ], 
                
                            filebrowserBrowseUrl : 'ckfinder/ckfinder.html',
                            filebrowserImageBrowseUrl : 'ckfinder/ckfinder.html?Type=Images',
                            filebrowserFlashBrowseUrl : 'ckfinder/ckfinder.html?Type=Flash',
                            filebrowserUploadUrl : 'ckfinder/core/connector/php/connector.php?command=QuickUpload&type=Files',
                            filebrowserImageUploadUrl : 'ckfinder/core/connector/php/connector.php?command=QuickUpload&type=Images',
                            filebrowserFlashUploadUrl : 'ckfinder/core/connector/php/connector.php?command=QuickUpload&type=Flash'
                
                            } );
                        //]]>
                    </script>
					</td>
	    		</tr>
				<tr>
				  <td>&nbsp;</td>
				  <td>&nbsp;</td>
	    </tr>
  		</table>
			<table width="99%" border="0" cellspacing="2" cellpadding="2">
	    		<tr>
		  			<td align="center"><input type="submit" class="button" name="Submit" value="ปรับปรุงบทเรียน" />
	  			    <input name="op" type="hidden" id="op" value="update" /></td>
	    		</tr>
	  		</table>
	  </form>
		</p>
	</div>
</div>
<?php
}else if($_GET['op']=='del'){
	mysql_query("delete from course where course_id='$_GET[course_id]'");
	redir("?mod=$mod&".time());exit;
}
?>